package com.thingple.usb.manager;

import android.annotation.SuppressLint;
import android.hardware.usb.UsbAccessory;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class UsbDeviceFilter {

    private static final String TAG = UsbDeviceFilter.class.getName();

    private final Map<String, Integer> filter = new HashMap<>();

    void registerSpec(String vendor, String product) {
        filter.put(genDeviceSpec(vendor, "" + product), 1);
    }
    @SuppressLint("DefaultLocale")
    private String genDeviceSpec(String vendor, String product) {
        return String.format("%s_%s", vendor, product);
    }

    boolean _check(String vendor, String product) {
        String spec = genDeviceSpec(vendor, product);
        Log.d(TAG, "spec:" + spec);
        boolean support = filter.containsKey(spec);
        Log.d(TAG, (support?"[support]":"[un_support]") + "spec:" + spec);
        return filter.containsKey(spec);
    }

    boolean check(String vendor, String product) {
        boolean ok = false;
        ok = _check(vendor, "" + product);

        if (!ok) {
            ok = _check(vendor, "*"); // 检查有没有通配符
        }
        return ok;
    }
    boolean check(@NotNull UsbAccessory device) {
        return check(device.getManufacturer(), device.getModel());
    }
}
